﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRD.Model.SRD.DTO;
using SRD.Model.SRD.BO;
using SRD.Classes;
using System.Data;
using SRD.Model;

namespace SRD.View.Views
{
    public partial class editStatusSubmissao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id_status_submissao"] != null)
                {
                    preencherCampos();
                }
            }
        }

        public void preencherCampos()
        {
            STATUS_SUBMISSAO statusSubmissao = new STATUS_SUBMISSAO();

            statusSubmissao.STATUS_SUBMISSAO_ID = Convert.ToInt32(Request.QueryString["id_status_submissao"].ToString());

            STATUS_SUBMISSAO statusSubmissaoExistente = new StatusSubmissaoBO().findById(statusSubmissao);

            txtNome.Text = statusSubmissaoExistente.DESCRICAO;
            if (statusSubmissaoExistente.ATIVO.Equals('S'))
            {
                CheckBoxAtivo.Checked = true;
            }
            else
            {
                CheckBoxAtivo.Checked = false;
            }
        }

        protected void BtSalvar_Click(object sender, ImageClickEventArgs e)
        {
            Usuario usuario = (Usuario)Session["usuario"];
            if (Request.QueryString["id_status_submissao"] != null)
            {
                STATUS_SUBMISSAO statusSubmissao = new STATUS_SUBMISSAO();
                statusSubmissao.STATUS_SUBMISSAO_ID = Convert.ToInt32(Request.QueryString["id_status_submissao"].ToString());
                statusSubmissao.DESCRICAO = txtNome.Text;
                statusSubmissao.EMPRESA_ID = usuario.empresa;
                if (CheckBoxAtivo.Checked)
                {
                    statusSubmissao.ATIVO = 'S';
                }
                else
                {
                    statusSubmissao.ATIVO = 'N';
                }
                String resposta = new StatusSubmissaoBO().update(statusSubmissao);
                Session["mensagem"] = resposta;
            }
            else
            {
                STATUS_SUBMISSAO statusSubmissao = new STATUS_SUBMISSAO();
                statusSubmissao.DESCRICAO = txtNome.Text;
                statusSubmissao.EMPRESA_ID = usuario.empresa;
                if (CheckBoxAtivo.Checked)
                {
                    statusSubmissao.ATIVO = 'S';
                }
                else
                {
                    statusSubmissao.ATIVO = 'N';
                }

                String resposta = new StatusSubmissaoBO().insert(statusSubmissao);
                Session["mensagem"] = resposta;                
            }
            Response.Redirect("listStatusSubmissao.aspx");
        }

        protected void BtVoltar_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("listStatusSubmissao.aspx");
        }
    }
}